package com.zcf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zcf.constant.Constants;
import com.zcf.dto.PasswordDto;
import com.zcf.mapper.UserMapper;
import com.zcf.pojo.Result;
import com.zcf.pojo.User;
import com.zcf.service.UserService;
import com.zcf.utils.JwtUtil;
import com.zcf.utils.Md5Util;
import com.zcf.utils.ThreadLocalUtil;
import com.zcf.vo.userVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zcf
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserMapper userMapper;

    @Override
    public Result register(String username, String password) {
        User user = userMapper.ByUserName(username);
        if (user != null) {
            return Result.error("用户已存在");
        }
        //用户不存在，可以注册
        //1-密码加密
        String md5PassWord = Md5Util.getMD5String(password);
        //2-注册
        userMapper.register(username, md5PassWord);
        return Result.success();
    }


    @Override
    public Result login(String username, String password) {
        User user = userMapper.ByUserName(username);
        if (user == null) {
            return Result.error("用户未注册");
        }
        boolean b = Md5Util.checkPassword(password, user.getPassword());
        if (!b) {
            return Result.error("密码错误");
        }
        //生成token，返回
        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        String token = JwtUtil.genToken(map);
        //保存令牌到redis
        stringRedisTemplate.opsForValue().set(Constants.LOGIN_KEY + token, token, 1, TimeUnit.HOURS);

        return Result.success(token);
    }


    @Override
    public Result userInfo(String username) {
        User user = userMapper.ByUserName(username);
        userVo userVo = new userVo();
        BeanUtils.copyProperties(user, userVo);

        return Result.success(userVo);
    }


    @Override
    public Result update(User user) {
        userMapper.update(user);
        return Result.success();
    }

    @Override
    public Result updatePassword(PasswordDto passwordDto,String token) {
        String old_pwd = passwordDto.getOld_pwd();
        String new_pwd = passwordDto.getNew_pwd();
        String re_pwd = passwordDto.getRe_pwd();
        //1先判断是否为空
        if(StrUtil.isBlank(old_pwd)||StrUtil.isBlank(new_pwd)||StrUtil.isBlank(re_pwd)){
            return Result.error("密码参数有空值");
        }
        //2.判断旧密码是否输入正确
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userMapper.ByUserName(username);
        if(!user.getPassword().equals(Md5Util.getMD5String(old_pwd))){
            return Result.error("旧密码输入错误");
        }
        //3.判断新密码和再次输入的密码是否一致
        if(!new_pwd.equals(re_pwd)){
            return Result.error("两次密码不一致");
        }
        User user2=new User();
        Integer id = (Integer) map.get("id");
        user2.setId(id);
        user2.setPassword(Md5Util.getMD5String(new_pwd));
        userMapper.updatePwd(user2);

        stringRedisTemplate.delete(Constants.LOGIN_KEY+token);
        return Result.success();
    }




    @Override
    public void updateUrl(String avatarUrl) {
        //获取当前用户id
       Map<String,Object> map= ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.url(avatarUrl,id);
    }
}
